package com.javaxxf.controller;

import com.javaxxf.pojo.User;
import com.javaxxf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author xuxiaofei
 * @date 2021年09月14日下午1:35
 * @联系作者：如果你有什么不懂，可以加作者微信 xxf960513 一起交流学习
 */
@RestController
@Api(tags = "用户操作api")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public String test( ){
    return "热部署成功了";
    }




    @PutMapping("user")
    @ApiOperation(value = "添加用户")
    @ApiImplicitParam(name = "user",value = "用户实体类",dataType ="User")
    public String addUser(@RequestBody User user){
        int save = userService.save(user);
        if (save>0){
            return "添加成功";
        }else {
            return "添加失败";
        }

    }

    @PostMapping("user")
    @ApiOperation(value = "根据id更新用户信息")
    @ApiImplicitParam(name = "user",value = "用户实体类",dataType ="User")
    public String updateUser(@RequestBody User user){
        int save = userService.update(user);
        if (save>0){
            return "更新成功";
        }else {
            return "更新失败";
        }

    }

    @DeleteMapping("user/{uid}")
    @ApiOperation(value = "根据id删除用户")
    @ApiImplicitParam(name = "uid",value = "用户id",dataType = "Integer")
    public String deleteUserById(@PathVariable Integer uid){
        int save = userService.delete(uid);
        if (save>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @ApiOperation(value = "根据用户id查询用户记录")
    @ApiImplicitParam(name = "uid",value = "查询参数",required = true)
    @GetMapping("user/{uid}")
    public User findUserById(@PathVariable Integer uid){
        return userService.findUserById(uid);
    }

}
